#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qqtcore.h"
#include "qqtframe.h"
#include "sfilecopy.h"

MainWindow::MainWindow ( QWidget* parent ) :
    QMainWindow ( parent ),
    ui ( new Ui::MainWindow )
{
    ui->setupUi ( this );
    setMinimumSize ( 1024, 600 );
    ui->lineEdit->installEventFilter ( this );
    connect ( this, SIGNAL ( clickBtn() ),
              this, SLOT ( on_pushButton_clicked() ), Qt::QueuedConnection );
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    ui->tx1->clear();
    if ( ui->lineEdit->text().isEmpty() )
    {
        ui->tx1->append ( "主类名（也是库名）不能为空" );
        return;
    }
    if ( ui->lineEdit->text() == "template" )
    {
        ui->tx1->append ( "主类名（也是库名）不能为template" );
        return;
    }

    //目标 库名
    QString className = ui->lineEdit->text();

    //创建目标文件夹目录结构
    ui->tx1->append ( "正在创建目录结构。。。。" );
    QDir root = QDir ( qApp->applicationDirPath() ).relativeFilePath ( QString ( "%1" ).arg ( CONFIG_ROOT ) );

    QString rootPath = root.absolutePath();
    QString tempName = "QQtTemplateLibrary";

    QString projDir = "a1-libproject";
    root.mkdir ( projDir );
    root.cd ( projDir );

    root.mkdir ( className );
    root.cd ( className );
    root.mkdir ( "demo" );
    root.mkdir ( "example" );
    root.mkdir ( "test" );
    root.mkdir ( "licence" );
    root.mkdir ( "screenshot" );
    root.mkdir ( "src" );

    //multi-link直接拷贝，Lib在自己的工程管理里面需要使用自己的。利于+submodule，利于版本管理。
    //加上这个文件夹，利于直接编译过。后续我会+submodule，使用持续更新的multi-link，不会使用这个。
    ui->tx1->append ( "正在拷贝multi-link文件夹。。。" );
    SFileCopy sf;
    sf.copyDirectoryFiles ( QString ( "template/%1/multi-link" ).arg ( tempName ),
                            QString ( "%1/%2/multi-link" ).arg ( projDir ).arg ( className ), true );

    //创建目标文件 这一步可以省略，在下边做。

    //把模板文件里的 关键字符串 进行合理更新，保存到目标文件
    ui->tx1->append ( "正在生成新工程文件。。。" );
    QByteArray bKey = className.toUpper().toLocal8Bit();
    QByteArray cKey = className.toLocal8Bit();
    QByteArray lKey = className.toLower().toLocal8Bit();

    QByteArray sBytes;
    QByteArray tBytes;

    QString sFile = QString ( "template/%1/%1.pro" ).arg ( tempName );
    QString tFile = QString ( "%1/%2/%2.pro" ).arg ( projDir ).arg ( className );
    ui->tx1->append ( tFile );
    QFile f0 ( sFile );
    f0.open ( QFile::ReadOnly );
    sBytes = f0.readAll();
    f0.close();
    tBytes = sBytes;
    tBytes = tBytes.replace ( tempName, cKey );
    tBytes = tBytes.replace ( tempName.toUpper(), bKey );
    tBytes = tBytes.replace ( tempName.toLower(), lKey );
    QFile f1 ( tFile );
    f1.open ( QFile::Truncate | QFile::WriteOnly );
    f1.write ( tBytes );
    f1.close();

    QDir d ( rootPath );
    d.cd ( QString ( "template/%1/src" ).arg ( tempName ) );
    foreach ( QFileInfo fi, d.entryInfoList() )
    {
        if ( !fi.isFile() )
            continue;
        sFile = QString ( "template/%1/src/%2" ).arg ( tempName ).arg ( fi.fileName() );
        //必须按照相应大小写改变，保持风格。
        tFile = QString ( "%1/%2/src/%3" ).arg ( projDir ).arg ( className )
                .arg ( fi.fileName().replace ( tempName, cKey )
                       .replace ( tempName.toUpper(), bKey ).replace ( tempName.toLower(), lKey ) );
        ui->tx1->append ( tFile );
        f0.setFileName ( sFile );
        f0.open ( QFile::ReadOnly );
        sBytes = f0.readAll();
        f0.close();
        tBytes = sBytes;
        tBytes = tBytes.replace ( tempName, cKey );
        tBytes = tBytes.replace ( tempName.toUpper(), bKey );
        tBytes = tBytes.replace ( tempName.toLower(), lKey );
        f1.setFileName ( tFile );
        f1.open ( QFile::Truncate | QFile::WriteOnly );
        f1.write ( tBytes );
        f1.close();

    }

}

bool MainWindow::eventFilter ( QObject* watched, QEvent* event )
{
    //为了下边的打印方便
    if ( event->type() == QEvent::Paint )
        return QMainWindow::eventFilter ( watched, event );

    //这句判断没意义，除非Qt出bug了。
    //if ( watched != ui->lineEdit )
    //return QMainWindow::eventFilter ( watched, event );

    if ( event->type() == QEvent::KeyRelease )
    {
        QKeyEvent* e = ( QKeyEvent* ) event;

        switch ( e->key() )
        {
            case Qt::Key_Enter:
            case Qt::Key_Return:
            {
                emit clickBtn();
                e->accept();
                return true;
            }
            break;
            default:
                break;
        }
    }
    return QMainWindow::eventFilter ( watched, event );
}
